package com.financing.wap.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class FileGzipUtil {
	static Logger logger = Logger.getLogger(FileGzipUtil.class);
    public static String fileGzip(String[] files) throws IOException{
    	String zipFileName = UUID.randomUUID().toString().replace("-","")+".zip";
    	if(files!=null&&files.length>0){
    		File file=new File(File.separator+"disk"+File.separator+files[0]);
    		zipFileName=file.getParentFile().getAbsoluteFile()+File.separator+zipFileName;
    	}
        File zipFile = new File(zipFileName);
        logger.info("zipFileName:"+zipFileName);
        InputStream input = null;
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
        zipOut.setEncoding("GBK");  
        for (int i = 0; i < files.length; i++) {
        	File file=new File(File.separator+"disk"+File.separator+files[i]);
        	logger.info("file zip name:"+file.getPath());
            input = new FileInputStream(file);
            zipOut.putNextEntry(new ZipEntry(file.getName()));
            int temp = 0;
            while ((temp = input.read()) != -1) {
                zipOut.write(temp);
            }
            input.close();
        }
        zipOut.close();
        return zipFileName;
    }

}
